package com.chenqi.myblog.controller.admin;

import com.chenqi.myblog.model.User;
import com.chenqi.myblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @description: 用户控制器
 * @program: myblog
 * @author: ChenQi
 * @create: 2021-02-2021/2/2-09-50
 */
@Controller
@RequestMapping("/admin")
public class AUserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登陆
     * @param username
     * @param modelAndView
     * @param session
     * @return
     */
    @PostMapping("/login")
    public ModelAndView login(@RequestParam String username,
                              @RequestParam String password,
                              ModelAndView modelAndView,
                              HttpSession session){
        User user = userService.findUserByUsername(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
                session.setAttribute("user", user);
                modelAndView.setViewName("redirect:/admin/type_list");
                return modelAndView;
            }
        }
        modelAndView.setViewName("redirect:/login.html?msg=username or password is wrong!");
        return modelAndView;
    }
}
